package com.koron.car.web.applycar.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.koron.bean.base.Response;
import com.koron.car.web.applycar.bean.entity.DriverAccident;
import com.koron.car.web.applycar.bean.query.DriverAccidentQuery;
import com.koron.car.web.applycar.service.DriverAccidentService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author twcao
 * @title: DriverAccidentController
 * @projectName GDH_EAM_V2
 * @description: 车辆使用-出险
 * @date 2022/3/918:46
 */
@Slf4j
@RestController
@RequestMapping("/driver/accident")
@Api(tags = "车辆管理-出险")
public class DriverAccidentController {

    @Autowired
    private DriverAccidentService accidentService;

    @GetMapping("/selectOne")
    @ApiOperation("根据id查询出险信息")
    public Response<DriverAccident> selectOne(@RequestParam String id) {
        log.info("根据id查询出险信息， id = {}", id);
        DriverAccident bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), accidentService, "queryById", DriverAccident.class, id);
        return Response.ok(bean);
    }

    @GetMapping("/getByApplyId")
    @ApiOperation("根据申请单查询所有的出险信息")
    public Response<List<DriverAccident>> getByApplyId(String applyId) {
        log.info("根据申请单查询所有的出险信息， applyId = {}", applyId);
        List<DriverAccident> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), accidentService, "getByApplyId", List.class, applyId);
        return Response.ok(beans);
    }

    @GetMapping("/queryList")
    @ApiOperation("出险信息列表查询")
    public Response<List<DriverAccident>> queryList(DriverAccidentQuery query) {
        log.info("出险信息列表查询， query = {}", query);
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<DriverAccident> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), accidentService, "queryList", List.class, query);
        return Response.ok(beans);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("创建/编辑出险信息")
    public Response<DriverAccident> saveOrUpdate(@RequestBody DriverAccident accident) {
        log.info("创建/编辑出险信息， accident = {}", JSON.toJSONString(accident));
        DriverAccident bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), accidentService, StringUtils.isBlank(accident.getId()) ? "insert" : "update", DriverAccident.class, accident);
        return Response.ok(bean);
    }

    @GetMapping("/deleteById")
    @ApiOperation("删除出险记录")
    public Response<Boolean> deleteById(String accidentId) {
        log.info("删除出险记录， accidentId = {}", accidentId);
        Boolean flag = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), accidentService, "deleteById", Boolean.class, accidentId);
        return Response.ok(flag);
    }
}
